package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.entity.Employees;
import com.etc.entity.Records;
import com.etc.service.IEmployeesService;
import com.etc.service.IRecordsService;
import com.etc.service.impl.EmployeesServiceImpl;
import com.etc.service.impl.RecordsServiceImpl;
import com.etc.util.BaiduAiUtil;
import com.etc.util.GetPositionUtil;
import com.etc.util.MD5Util;
import com.google.gson.Gson;
import com.sun.org.apache.bcel.internal.generic.NEW;
/**
 * Servlet implementation class FaceLoginController
 */
@WebServlet("/FaceController")
public class FaceController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IRecordsService rs = new RecordsServiceImpl();
	private BaiduAiUtil ai = new BaiduAiUtil();
	private IEmployeesService es = new EmployeesServiceImpl();
	private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    public FaceController() {
        super();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	doPost(req, resp);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String image = request.getParameter("image");
		image = image.substring(22);
		if(!ai.faceCheck(image)) {
			response.getWriter().print("检测出图中有多张人脸~请重新拍照");
			response.getWriter().close();
			return;
		}
		String op = request.getParameter("op");
		System.out.println(image);
		HttpSession session = request.getSession();
		if("search".equals(op)) {
			search(request, response,session,image);
		}else if("register".equals(op)) {
			register(request, response,image);
		}
	}
	
	/**
	 * 人脸搜索
	 * @param image 前台传来的base64格式的图片
	 * @throws IOException
	 */
	private void search(HttpServletRequest request, HttpServletResponse response,HttpSession session,String image) throws IOException {
		response.setContentType("application/json;utf-8");
		PrintWriter out = response.getWriter();
		Map<String, String> map = new HashMap<>();
		Employees emp = (Employees)session.getAttribute("emp");
		String user_id = ai.faceSearch(image);
		System.out.println("user_id:" + user_id);
		if(user_id != null && user_id.equals(String.valueOf(emp.getEmpId()))) {
			int empId = Integer.parseInt(user_id);
			Records record = rs.getCurrentDayRecord(empId);
			
			if(record!=null) {
				if(record.getEndTime() == null) {
					rs.clockOut(record);
					map.put("workTime", "下班时间18:00");
					map.put("result", "下班打卡成功~~");
				}else {
					map.put("result", "今日已打卡,无需再打~~");
				}
				
			}else {
				rs.clockIn(empId);
				map.put("workTime", "上班时间09:00");
				map.put("result","上班打卡成功~~");
			}
			map.put("clockTime", "打卡时间"+sdf.format(new Date()));
			
			map.put("address", GetPositionUtil.getAddrName());
			
		}else {
			map.put("result", "人脸识别不通过,请重试~~");
		}
		Gson gson = new Gson();
		String json = gson.toJson(map);
		out.print(json);
		out.close();
		
	}
	
	private void register(HttpServletRequest request, HttpServletResponse response, String image) throws IOException {
		PrintWriter out = response.getWriter();
		String empName = request.getParameter("empName");
		String empPwd = request.getParameter("empPwd");
		System.out.println(empPwd   +  empName);
		Employees emp = es.login(empName, empPwd);
		System.out.println("emp:" + emp);
		if(emp != null) {
			String id = String.valueOf(emp.getEmpId());
			if(!ai.faceExist(id)) {
				boolean flag = ai.faceRegister(id, image);
				if(flag) {
					out.print("人脸注册成功~~");
				}else {
					out.print("人脸注册失败,请重试~~");
				}
				out.close();
				return;
			}
			out.print("您要注册的人脸id已存在~~");
		}else {
			out.print("找不到用户~~");
		}
		out.close();
	}
	
	
}
